package dyyx;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;

public class MvelDemo {

	public static void main(String[] args) throws Exception {	
		System.out.println("Func001,call static method test");	
		
		
		String expression = " double x = dyyx.Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";		
		expression = "import dyyx.Utils; double x = Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";
		expression = "import dyyx.Utils; if(a>0){a=3;} ; double x = Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";
		
		// 无论是 引号 还是 双引号 都不会做变量解析
		// expression = "String str = '$a';return str";
		// expression = "String str = \"$a\"; System.out.println(\"str=\"+str);return str";

		Map<String,Object> params = new HashMap<String,Object>();
		params.put("a", 1);
		params.put("b", 2);
		params.put("c", 3);
		// 不报错
		params.put("c", "3");
		// params.put("c", "xxx");

		// 变量不存在报错
		// params.remove("c");

		// 直接执行
		Object result = null;
		result = MVEL.eval(expression, params);

		System.out.println(result);
		System.out.println(result.getClass());
	
		// 编译后执行
		Serializable compiledExpression = null;
		// 表达式编译
		compiledExpression = MVEL.compileExpression(expression);
		// params.put("c", 9);
		result = MVEL.executeExpression(compiledExpression, params);
		System.out.println(result);
		
	
		
	}
	
	
	
	
	
}
